<div class="row">
  <div class="col-md-12 col-xs-12">

    <%= render 'nav' %>

    <div class="row m-t-10 m-b-10">
      <div class="col-md-2">
        <%= link_to '添加销售记录', new_sale_path, class: 'btn btn-default' if can? :create, Sale %>
      </div>
      <div class="col-md-10">
        <%= search_form_for @q, html: {class: 'pull-right form-inline'} do |f| %>
            <div class="form-group">
              <label>销售时段</label>
              <%= text_field_tag :'q[sold_at_gteq]', @q.sold_at_gteq.try(:strftime, '%F'), class: 'form-control', style: 'width: 100px;' %>
              <%= text_field_tag :'q[sold_at_lteq]', @q.sold_at_lteq.try(:strftime, '%F'), class: 'form-control', style: 'width: 100px;' %>
            </div>
            <div class="form-group">
              <label>销售姓名</label>
              <%= f.search_field :staff_name_cont, class: 'form-control', style: 'width: 100px;' %>
            </div>
            <div class="form-group">
              <label>产品名称</label>
              <%= f.search_field :product_name_cont, class: 'form-control', style: 'width: 100px;' %>
            </div>
            <%= f.button class: 'btn btn-default' do %>
                <i class="glyphicon glyphicon-search"></i> 查询
            <% end %>
        <% end %>
      </div>
    </div>

    <table class="table table-striped table-bordered">
      <thead>
      <tr>
        <th>编号</th>
        <th>员工</th>
        <th>客户</th>
        <th>产品</th>
        <th>数量</th>
        <th>销售时间</th>
        <th>产品到期时间</th>
        <th>获得奖励</th>
        <th>操作</th>
      </tr>
      </thead>

      <tbody>
      <% @sales.each do |sale| %>
          <tr>
            <td><%= sale.id %></td>
            <td><%= link_to sale.staff.name, staff_path(sale.staff) %></td>
            <td><%= link_to sale.customer.name, customer_path(sale.customer) %></td>
            <td><%= link_to sale.product.name, product_path(sale.product) %></td>
            <td><%= sale.product_number %></td>
            <td><%= sale.sold_at.strftime('%F') %></td>
            <td><%= sale.product_expired_at.try(:strftime, '%F') %></td>
            <td><%= sale.sales_reward %></td>
            <td>
              <%= link_to '详情', sale if can? :read, sale %>
              <%= link_to '编辑', edit_sale_path(sale) if can? :update, sale %>
            </td>
          </tr>
      <% end %>
      </tbody>
    </table>

    <%= render partial: 'shared/paginate', locals: {collection: @sales} %>

    <div class="well well-sm">
      温馨提示：
      <ul>
        <li>
          销售人员获得奖励 = 销售产品.价格 * 销售奖励 * 产品数量
        </li>
        <li>
          销售记录添加或编辑完成后，销售人员获得奖励不会因为产品价格、产品奖励之后的修改而改变。
        </li>
      </ul>
    </div>
  </div>
</div>

<% content_for :javascript do %>
    <script>
      $(function(){
          $('#q_sold_at_gteq').datepicker({
              format: "yyyy-mm-dd",
              language: "zh-CN",
              weekStart: 1,
              autoclose: true,
              todayHighlight: true
          });
          $('#q_sold_at_lteq').datepicker({
              format: "yyyy-mm-dd",
              language: "zh-CN",
              weekStart: 1,
              autoclose: true,
              todayHighlight: true
          });
      });
    </script>
<% end %>
